package com.whalefin.service;


import com.whalefin.domain.Advertise;
import com.whalefin.domain.LeaveMessage;
import com.whalefin.domain.Reply;
import com.whalefin.persistence.AdvertiseMapper;
import com.whalefin.persistence.LeaveMessageMapper;
import com.whalefin.persistence.ReplyMapper;
import com.whalefin.service.tools.BooleanResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;


@Controller
public class ReplyService {

    @Autowired
    private ReplyMapper replyMapper;
    @Autowired
    private LeaveMessageMapper leaveMessageMapper;
    @Autowired
    private AdvertiseMapper advertiseMapper;
    @Autowired
    private iOSPushNotificationService pushService;

    /**
     * 评论回复
     * @param userId 评论人id
     * @param targetUserId 回复人id
     * @param leaveMessageId 留言id
     * @param advertiseId 广告id
     * @param replyContent 评论内容
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,value = "/replyMsg")
    public @ResponseBody
    BooleanResult replyMsg(@RequestParam Integer userId,@RequestParam Integer targetUserId,
                                                    @RequestParam Integer leaveMessageId,@RequestParam Integer advertiseId,
                                                        @RequestParam String replyContent){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        //组装数据
        Reply reply = new Reply();
        reply.setUserId(userId);
        reply.setReplyContent(replyContent);
        reply.setCreatetime(sdf.format(date));
        if(leaveMessageId != 0){
            reply.setLeaveMessageId(leaveMessageId);//评论留言
        }else {
            reply.setLeaveMessageId(0);//不评论留言设为0
        }
        if(advertiseId != 0){
            reply.setAdvertiseId(advertiseId);//评论广告
        }else {
            reply.setAdvertiseId(0);//不评论广告设为0
        }
        if(userId != 0 && targetUserId == 0){//targetUserId=0为评论
            reply.setTargetUserId(0);
        }else if(userId != 0 && targetUserId != 0){//targetUserId!=0为回复
            reply.setTargetUserId(targetUserId);
        }
//结果判断
        int result = replyMapper.replyMsg(reply);

        if(leaveMessageId != 0){//isRead为0  未读
            LeaveMessage leaveMessage = leaveMessageMapper.queryLeaveMsgByLeaveMessageId(leaveMessageId);
            if(targetUserId == 0){
                targetUserId = leaveMessage.getUserId();
            }
            int insertResult = replyMapper.insertCirclePush(leaveMessageId,0,replyContent,userId,targetUserId,sdf.format(date),0);
            if(insertResult > 0){
                pushService.personalPush(targetUserId,"您有一条新信息","500");
            }
        }

        if(advertiseId != 0){
            Advertise advertise = advertiseMapper.queryAdvertisesByAdvertiseId(advertiseId);
            if(targetUserId == 0){
                targetUserId = advertise.getUserId();
            }
            int insertResult = replyMapper.insertCirclePush(0,advertiseId,replyContent,userId,targetUserId,sdf.format(date),0);
            if(insertResult > 0){
                pushService.personalPush(targetUserId,"您有一条新信息","500");
            }
        }

        if (result > 0){
            return new BooleanResult(true,"评论成功!");
        }else {
            return new BooleanResult(false,"评论失败!");
        }
    }


}